<?
class GDataGrid extends gravy{
	public $gravy;
	public $dataKey;
	public $columns=array();
	public $errorMessages=array();
	public $actionOptions=array();
	public $actionOptionsDescription=array();
	public $displaySummary=true;
	public $displayPaging=true;

	/**
	 * <b>Class Constructor</b>
	 * @param application $gravy (required): the reference to the application's root class.
	 * @param String $dataKey (optional): the key within the $gravy->data array that will be used to generate the Data Grid.
	 * @return void
	 */
	public function __construct(application &$gravy,$dataKey=NULL){
		parent::__construct();
		$this->gravy=$gravy;
		$this->dataKey=$dataKey;
		$this->setErrorMessage(GDataGridErrorType::NODATA,"<p>No records were found in the system using those search parameters.</p>");
	}

	/**
	 * <b>addColumn</b>
	 * This function adds a data grid column to the data grid output.
	 * @param GDataGridColumn $column (required): the GDataGridColumn object that defines the column's attributes.
	 * @return void
	 */
	public function addColumn(GDataGridColumn $column){
		$this->columns[]=$column;
	}

	/**
	 * <b>getActionOptions</b>
	 * This function returns the action options for a given Data Key.
	 * @param String $dataKey (required): the Data Key that the action options relate to/will be output for.
	 * @return GDataGridActionOptions $actionOptions (required): the GDataGridActionOptions object that defines the action options's attributes.
	 */
	public function getActionOptions($dataKey){
		$result=false;
		if(array_key_exists($dataKey,$this->actionOptions)){
			$result=$this->actionOptions[$dataKey];
		}
		return($result);
	}

	/**
	 * <b>addActionOptions</b>
	 * This function adds an action column to the action options list
	 * @param String $dataKey (required): the Data Key that the action options relate to/will be output for.
	 * @param GDataGridActionOptions $actionOptions (required): the GDataGridActionOptions object that defines the action options's attributes.
	 * @return void
	 */
	public function addActionOptions($dataKey,GDataGridActionOptions $actionOptions,$description=NULL){
		$this->actionOptions[$dataKey]=$actionOptions;
		if($description!==NULL){
			$this->actionOptionsDescription[$dataKey]=$description;
		}
	}

	public function getActionOptionsDescription($dataKey,$data){
		$result=false;
		if($text=$this->actionOptionsDescription[$dataKey]){
			$result=$text;
			if(preg_match_all('/##([^#]+)##/',$result,$matches)){
				$matches=$matches[1];
				for($c=0;$c<count($matches);$c++){
					$match=$matches[$c];
					$replace="";
					if($match=="id"){
						$replace=model::numencrypt($data["id"],$data["dateAdded"]);
					}else{
						$replace=$data[$match];
					}
					$result=preg_replace("/##$match##/",$replace,$result);
				}
			}
		}
		return($result);
	}

	/**
	 * <b>getErrorMessage</b>
	 * This function gets a data grid error message.
	 * @param GDataGridErrorType $errorType (required): the type of error that the message will cater for.
	 * @return string $result: the error message text to be output.
	 */
	public function getErrorMessage($errorType){
		return($this->errorMessages[$errorType]);
	}
	/**
	 * <b>setErrorMessage</b>
	 * This function sets a customised data grid error message.
	 * @param GDataGridErrorType $errorType (required): the type of error that the message will cater for.
	 * @param String $message (required): the error message to display when a GDataGridErrorType error is thrown.
	 * @return void
	 */
	public function setErrorMessage($errorType,$message){
		$this->errorMessages[$errorType]=$column;
	}

	/**
	 * <b>getDisplaySummary</b>
	 * This function gets the display summary variable which determines whether the summary data is shown or not.
	 * @return Boolean $result: true to show the summary (default), false to hide it.
	 */
	public function getDisplaySummary(){
		return($this->displaySummary);
	}
	/**
	 * <b>setDisplaySummary</b>
	 * This function sets the display summary variable which determines whether the summary data is shown or not.
	 * @param Boolean $value (required): true to show the summary (default), false to hide it.
	 * @return void
	 */
	public function setDisplaySummary($value=true){
		$this->displaySummary=$value;
	}

	/**
	 * <b>getDisplayPaging</b>
	 * This function gets the display paging variable which determines whether the paging links are shown or not.
	 * @return Boolean $result: true to show the paging links (default), false to hide it.
	 */
	public function getDisplayPaging(){
		return($this->displaySummary);
	}
	/**
	 * <b>setDisplayPaging</b>
	 * This function sets the display paging variable which determines whether the paging links are shown or not.
	 * @param Boolean $value (required): true to show the paging links (default), false to hide it.
	 * @return void
	 */
	public function setDisplayPaging($value=true){
		$this->displaySummary=$value;
	}

	/**
	 * <b>getOutput</b>
	 * This function generates the Data Grid HTML and returns it to the caller as a string.
	 * @param void
	 * @return String $result: the Data Grid HTML output
	 */
	public function getOutput(){
		$result="";

		$sorter=new GDataGridSorter($this->gravy);
		$sorter->setHeaderClasses("col_blank","col_asc","col_desc");

		$rows=$this->gravy->data[$this->dataKey];
		if(is_array($rows)&&count($rows)>0){
			$paging=(pager::recordCount()>0);
			if($paging&&$this->getDisplaySummary()){
				$result.="<p class=\"pager-summary pager-summary-top\">Displaying ".pager::recordsFrom()." to ".pager::recordsTo()." of ".pager::recordCount()." results.</p>".chr(13);
				if($this->getDisplayPaging()){
					$result.=pager::links();
				}
			}

			$result.='<table cellpadding="0" cellspacing="0" border="0" class="data-table" id="gdg-'.$this->dataKey.'">'.chr(13);
			$result.='	<thead>'.chr(13);

			$header='		<tr>'.chr(13);
			for($c=0;$c<count($this->columns);$c++){
				$column=$this->columns[$c];
				$header.='			<td'.$column->getWrappingHtml().' align="'.$column->getAlign().'"><a href="'.$sorter->getUrl($column->getClass()).'" class="'.$sorter->getClass($column->getClass()).'">'.$column->getText().'</a></td>'.chr(13);
			}
			$header.='		</tr>'.chr(13);

			$result.=$header;
			$result.='	</thead>'.chr(13);

			//Generate the Rows
			$result.='	<tbody>'.chr(13);
			for($c=0;$c<count($rows);$c++){
				$row=$rows[$c];
				$result.='		<tr>'.chr(13);
				for($d=0;$d<count($this->columns);$d++){
					$column=$this->columns[$d];
					$result.='			<td'.$column->getWidthHtml().$column->getWrappingHtml().' align="'.$column->getAlign().'">'.chr(13);
					$actionOptions=$this->getActionOptions($column->getDataField());
					if($actionOptions){
						if($actionOptions->getType()==GDataGridActionOptionType::INLINE_LINKS){
							$result.='				<p class="row_title">'.$column->getDataFieldHtml($row).'</p>'.chr(13);
							if($data=$this->getActionOptionsDescription($column->getDataField(),$row)){
								$result.='				<p class="row_description">'.$data.'</p>'.chr(13);
							}
							$result.='				<p class="row_options">'.chr(13);
							$options=$actionOptions->getOptions();
							for($e=0;$e<count($options);$e++){
								$result.='					'.$options[$e]->toHtml($row).chr(13);
							}
							$result.='				</p>'.chr(13);
						}
					}elseif($column instanceof GDataGridImage){
						$result.='				'.$column->toHtml($row).chr(13);
					}else{
						$result.='				'.$column->getDataFieldHtml($row).chr(13);
					}
					$result.='			</td>'.chr(13);
				}
				$result.='		</tr>'.chr(13);
			}
			$result.='	</tbody>'.chr(13);

			//Output the Footer
			$result.='	<tfoot>'.chr(13);
			$result.=$header;
			$result.='	</tfoot>'.chr(13);

			$result.='</table>'.chr(13);
			$result.='<br />'.chr(13);

			if($paging&&$this->getDisplaySummary()){
				$result.="<p class=\"pager-summary pager-summary-bottom\">Displaying ".pager::recordsFrom()." to ".pager::recordsTo()." of ".pager::recordCount()." results.</p>".chr(13);
				if($this->getDisplayPaging()){
					$result.=pager::links();
				}
			}
		}else{
			$result.=$this->getErrorMessage(GDataGridErrorType::NODATA);
		}
//	$model=new model();
//	for($c=0;$c<count($companies);$c++){
//		$row=$companies[$c];
//		$row["id"]=$model->num_encrypt($row["company_id"],$row["companyDateAdded"]);
//		<a href="<?=WEB_ROOT? >staff/manage/edit/<?=$row["id"].$r;? >">edit details</a>

		return($result);
	}

	/**
	 * <b>addColumnGroups</b>
	 * This function gets and prints the Data Grid HTML and also returns it to the caller as a string.
	 * @param void
	 * @return String $result: the Data Grid HTML output
	 */
	public function addColumnGroups($tmp){
		
		return($result);
	}

	/**
	 * <b>printOutput</b>
	 * This function gets and prints the Data Grid HTML and also returns it to the caller as a string.
	 * @param void
	 * @return String $result: the Data Grid HTML output
	 */
	public function printOutput(){
		$result=$this->getOutput();
		print($result);
		return($result);
	}
};
?>